﻿@{
    ViewBag.Title = "权限配置";
}
<div style="margin: 20px 20px;">

    <div class="panel panel-primary" style="width: 300px; float: left;">
        <div class="panel-heading">
            用户组
            <label>
                单位：
                @Html.DropDownList("hospitalid", (IEnumerable<SelectListItem>)ViewBag.SELECT_HOSPTIAL, "", new { @class = "notnull" })
            </label>
        </div>
        <div class="panel-body form-inline">

            @Html.MyButton("btn_add_usergroup", "新增")

        </div>
        <table id="maingrid"></table>
    </div>
    <div style="float: left; width: 2px;">&nbsp;</div>
    <div class="panel panel-primary" style="float: left; width: 300px;">
        <div class="panel-heading">用户组未选成员</div>
        <div class="panel-body form-inline">
            @Html.MyLabel("双击行可选择成员加入用户组", "info")
        </div>
        <table id="maingrid_member_not"></table>

    </div>
    <div style="float: left; width: 2px;">&nbsp;</div>
    <div class="panel panel-primary" style="float: left; width: 300px;">
        <div class="panel-heading">用户组已选成员</div>
        <div class="panel-body form-inline">
            @Html.MyLabel("双击行从成员中移出用户组", "info")
        </div>
        <table id="maingrid_member"></table>

    </div>
    <div style="float: left; width: 2px;">&nbsp;</div>
    <div class="panel panel-primary" style="float: left; width: auto;">
        <div class="panel-heading">权限</div>
        <div class="panel-body form-inline">
            &nbsp;
        </div>

    </div>
</div>



@section script
{

    <script src="@Url.Content("~/Scripts/plugins/bootstrap-paginator.min.js")"></script>
    <script src="@Url.Content("~/Scripts/plugins/jquery.grid.js")"></script>

    <script type="text/javascript">


        function sch() {
            $("#maingrid").bootgrid("setGridParam", { hospid: $("#hospitalid").val() });
            $("#maingrid").bootgrid("reloadGrid", { page: 1 });
        }

        function sch_grpm(grpid) {
            $("#maingrid_member").bootgrid("setGridParam", { grpid: grpid });
            $("#maingrid_member").bootgrid("reloadGrid", { page: 1 });
            $("#maingrid_member_not").bootgrid("setGridParam", { grpid: grpid, hospid: $("#hospitalid").val() });
            $("#maingrid_member_not").bootgrid("reloadGrid", { page: 1 });
        }

        function closeAddpopover() {
            $('#btn_add_usergroup').popover('toggle');
        }

        function addUserGroup() {
            var ugn = $("#usergroupname").val();
            if (ugn) {
                $.post("/xt/saveaddusergroup", { grpname: ugn, hospid: $("#hospitalid").val() }, function () {
                    sch();
                    closeAddpopover();
                });
            } else {
                mainfn.msg("请输入用户组名称");
            }
        }

        $(function () {

            $('#btn_add_usergroup').popover({
                html: true,
                placement: "bottom",
                content: "<label>用户组名称:<input type='text' name='usergroupname' id='usergroupname'></label>" +
                    "<div class='btn-group'>" +
                    "<button class='btn btn-primary btn-sm'  onclick='addUserGroup()'>保存</button>" +
                    "<button class='btn btn-default btn-sm'  onclick='closeAddpopover()'>关闭</button>" +
                    "</div>"
            }).on('shown.bs.popover', function () {
                $("#usergroupname").focus();
            });

            $("#hospitalid").change(function () {
                sch();
            });

            function insertIntoSelectedGrid(row) {

                var ugp = $("#maingrid").bootgrid("getGridSelRow");
                if (ugp && ugp.USER_GROUP_ID) {
                    $.post("/xt/saveinsertugpm", { grpid: ugp.USER_GROUP_ID, userid: row.USER_ID }, function () {
                        var tr = "<tr>";
                        var idx = 0;
                        for (var d in row) {
                            if (idx == 0) {
                                tr += "<td align='center' style='width:0;display:none'>" + row[d] + "</td>";
                            } else {
                                tr += "<td align='center'>" + row[d] + "</td>";
                            }
                            idx++;
                        }
                        tr += "</tr>;";
                        $("#maingrid_member tbody").prepend(tr);
                    });
                }
            }



            function init(parameters) {

                $("#maingrid").bootgrid({
                    url: '/xt/usergrouplist?hospid=' + $("#hospitalid").val(),
                    headers: ["用户组ID", "用户组名称"],
                    cols: [
                        { id: 'USER_GROUP_ID', hidden: true },
                        { id: 'USER_GROUP_NAME' }
                    ],
                    ondbclick: function (idx, id, row) {

                    },
                    onclick: function (idx, id, row) {
                        sch_grpm(id);
                    }
                });

                $("#maingrid_member_not").bootgrid({
                    url: '/xt/usergroupmemberlist_not?hospid=' + $("#hospitalid").val(),
                    headers: ["用户id", "用户工号", "用户组名称"],
                    cols: [
                        { id: 'USER_ID', hidden: true },
                        { id: 'USER_NO' },
                        { id: 'USER_NAME' }
                    ],
                    ondbclick: function (idx, id, row) {
                        insertIntoSelectedGrid(row);
                        $("#maingrid_member_not").bootgrid("removeSelRows");
                    }
                });

                $("#maingrid_member").bootgrid({
                    url: '/xt/usergroupmemberlist',
                    headers: ["用户id", "用户工号", "用户组名称"],
                    cols: [
                        { id: 'USER_ID', hidden: true },
                        { id: 'USER_NO' },
                        { id: 'USER_NAME' }
                    ],
                    ondbclick: function (idx, id, row) {

                    },
                    onclick: function (idx, id, row) {

                    }
                });
            }

            init();
        });
    </script>
}